<?php

/**
 * This is the model class for table "post_resources".
 *
 * The followings are the available columns in table 'post_resources':
 * @property string $id
 * @property string $post_id
 * @property string $file_name
 * @property string $file_url
 */
class PostResources extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return PostResources the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{post_resources}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('post_id', 'length', 'max' => 11),
            array('file_name, file_url', 'length', 'max' => 255),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, post_id, file_name, file_url', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'post_id' => '文档',
            'file_name' => '附件名称',
            'file_url' => '附件URL',
        );
    }
    
    public function getResources($post_id)
    {
        $post_id = (int)$post_id;
        $data = $this->findAll("post_id=$post_id");
        $str = '';
        foreach($data as $val)
        {
            $link = CHtml::link($val->file_name, Yii::app()->baseUrl.'/'.$val->file_url, array('target' => '_blank'));
            $str .= "<li>$link</li>";
        }
        return "<ul>$str</ul>";
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('post_id', $this->post_id);
        $criteria->compare('file_name', $this->file_name, true);
        $criteria->compare('file_url', $this->file_url, true);

        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                ));
    }

}